<?php

/**
 * @category    H2
 * @package     
 * @subpackage  
 * @author      Nam Nguyen
 * @copyright   
 */
class Core {

  protected static $_contents = NULL;

  /**
   * @return  return  Array
   */
  public static function getRoutes() {
    Route::add('stardand', ':route/:mod/:con/:act/*', array(
        'mod' => 'core',
        'con' => 'index',
        'act' => 'index',
            ), array(
        'route' => '(admin|widget|ajax|browse)',
    ));
    Route::add('default', ':mod/:con/:act/*', array(
        'route' => 'browse',
        'mod' => 'core',
        'con' => 'index',
        'act' => 'index',
    ));
    Route::add('404', '*', array(
        'route' => 'browse',
        'mod' => 'core',
        'con' => 'index',
        'act' => 'notfound',
    ));
    return Route::all();
  }

  /**
   *
   * @param type $module
   * @param type $service
   * @return class_name 
   */
  public static function getService($module, $service = NULL) {
    if ($service == NULL) {
      $service = $module;
    }
    $class_name = $module . "_Service_" . $sevice;
    return new $class_name;
  }

  /**
   *
   * @param type $identity
   * @param type $module
   * @param type $service 
   */
  public static function getEntry($identity, $module, $service = NULL) {
    
  }

  /**
   *
   * @param   string  $str
   * @return  string
   */
  public static function inflect($str) {
    return str_replace(' ', '', ucwords(str_replace(array('.', '-'), ' ', $str)));
  }

  /**
   *
   * @param   string $string
   * @return  string
   */
  public static function deflect($str) {
    return strtolower(trim(preg_replace('/([a-z0-9])([A-Z])/', '\1-\2', $str), '-. '));
  }

  /**
   *
   * @param type $params
   * @return type 
   */
  public static function check($params) {
    $parts = array(
        self::inflect(@$params['mod']),
        'Controller',
        self::inflect($params['route']),
        self::inflect($params['con'])
    );
    $filename = PACKAGE_DIR . implode(DIRECTORY_SEPARATOR, $parts) . '.php';


    if (!file_exists($filename)) {
      return false;
    }

    require_once $filename;
    $class_name = implode('_', $parts);

    
    if (class_exists($class_name) && ($action = @$params['act']) && method_exists($class_name, $action . 'Action')) {
      
      return array($class_name, $action);
    }
    return false;
  }

  /**
   * 
   */
  public static function dispatch() {
    $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';


    $uri = str_replace(BASE_URL, '', $uri);

    $flag = false;
    $con = NULL;

    foreach (Core::getRoutes() as $route) {
      if (($params = $route->match($uri)) != NULL) {
        if (($parts = Core::check($params)) != NULL) {
          $flag = true;
          list($class_name, $action) = $parts;
          $con = Controller::factory($class_name, $params);
          $con->forward($action);
          break;
        }
      }
    }
  }

  /**
   *
   * @return type 
   */
  protected static function _getContainer() {
    $con = Controller::current();
    $pageName = $con->getPageName();
    $sql = "select * from core_contents where enabled=1 and 1 order by priority, container, ordering;";
    $rows = self::getDb()->fetchAll($sql);
    $result = array();
    foreach ((array) $rows as $row) {
      $result[$row['container']][] = $row;
    }
    return $result;
  }

  /**
   *
   * @param type $name
   * @return type 
   */
  public static function getContainer($name = NULL) {
    if (self::$_contents == NULL) {
      self::$_contents = self::_getContainer();
    }
    return $name == NULL ? self::$_contents : @self::$_contents[$name];
  }

  public static function renderWidget($class_name) {
    $widget=  new $class_name();
    $widget->indexAction();
    $widget->render();
  }

}